<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Validate;


class Proinfo extends Controller
{
    /**
     * 首页
    */
    public function prolist()
    {
        $condition1 =  Db::name('user')
            ->where('id', session('user_id'))
            ->value('id');
        if ((int) $condition1 == 1) {
        	$param = $this->request->get();
        	$where = [];
			if(!empty($param['pubTime'])){
				$where['pubTime'] = $param['pubTime'];
			}
			//根据pId分组，筛选出每组状态最新的记录
			$count = Db::name('proinfo')
	      	->count();

			$subQuery1 = Db::name('order')
				->order('status desc')
				// ->limit($count+2)	
			    ->buildSql();

			$subQuery2 = Db::table($subQuery1 . ' a')
			    ->group('a.pId')
			    ->buildSql();
			
			// var_dump($subQuery2);
            $data = Db::name('user')
            ->alias('a')
            ->join('proinfo b','b.u_id = a.id')
            ->join('cate c','c.id = b.cate_id')
            ->join([$subQuery2=> 'q'], 'q.pId = b.id')
            ->order('status asc')
            ->paginate(12);
            // var_dump($data);
	      	

	      	$this->assign('count', $count);
	        $this->assign('pros', $data);
	        return $this->fetch();
        }else{
            //根据pId分组，筛选出每组状态最新的记录
			$count = Db::name('proinfo')
			->where('u_id', session('user_id'))
	      	->count();

			$subQuery1 = Db::name('order')
				->order('status desc')
				->limit($count+1)
			    ->buildSql();

			$subQuery2 = Db::table($subQuery1 . ' a')
			    ->group('a.pId')
			    ->buildSql();
			
			// var_dump($subQuery2);
            $data = Db::name('user')
            ->alias('a')
            ->join('proinfo b','b.u_id = a.id')
            ->join('cate c','c.id = b.cate_id')
            ->join([$subQuery2=> 'q'], 'q.pId = b.id')
            ->where('u_id', session('user_id'))
            ->paginate(12);
           
	      	$this->assign('count', $count);
	        $this->assign('pros', $data);
	        return $this->fetch();
        }

    }

    //增加工单
    public function addPro()
    {
    	$request = $this->request;
    	
        if($request->isPost()){
            $post     = $request->post();
            $validate = validate('Proinfo');
            $res      = $validate->check($post);

            if ($res !== true) {
                $this->error($validate->getError());
                
            }else {
                $post['pubTime']   = date('Y-m-d H:i:s', time());
                $post['u_id'] = session('user_id');

                $db = Db::name('proinfo')
                    ->insert($post); 
                $oId = Db::name('proinfo')->getLastInsID();  
                Db::name('order')
                ->insert(['pId'=>$oId, 'resource'=>3, 'status'=>1, 'exp'=>'one']);//one作为未完成工单的标志

                $count1 = Db::name('user')
		        ->where('id', session('user_id'))
		        ->value('achPro');
			   	
			    $db3 = Db::name('user')
			        ->where('id', session('user_id'))
			        ->update(['achPro'=>($count1+1)]);

                $this->success('添加成功');
            }
        }else{
            return $this->fetch('add');
        }
       
    }

    //删除工单
    public function deletePro()
    {
        $id = $this->request->post('id');

        $count1 = Db::name('user')
        ->where('id', session('user_id'))
        ->value('achPro');

        $data = Db::name('user')
            ->alias('a')
            ->join('proinfo b','a.id = b.u_id')
            ->where('u_id', session('user_id'))
            ->update(['achPro'=>($count1-1)]);

        $delete1 = Db::name('proinfo')
	        ->where('id', $id)
	        ->delete();
	    $delete2 = Db::name('order')
	        ->where('pId', $id)
	        ->delete();

        $this->success('删除成功');
    }

    //追踪页
    public function detail($pId)
    {
    	$request = $this->request;

    	$exp[0]['exp'] = Db::name('order')
    		->field('exp')
    		->where('pId', $pId)
    		->where('status', 2)
    		->select();
    	$exp[1]['exp'] = Db::name('order')
    		->field('exp')
    		->where('pId', $pId)
    		->where('status', 3)
    		->select();

    	$time[0]['changeTime1'] = Db::name('order')
    		->field('changeTime1')
    		->where('pId', $pId)
    		->where('status', 2)
    		->select();
		$time[1]['changeTime2'] = Db::name('order')
    		->field('changeTime2')
    		->where('pId', $pId)
    		->where('status', 3)
    		->select();
    	// var_dump($exp[0]['exp']);
    	// var_dump($exp[1]['exp']);
    	// var_dump($time[0]['changeTime1']);
    	// var_dump($time[1]['changeTime2']);

    	$detail = Db::name('user')
            ->alias('a')
            ->join('proinfo b','b.u_id = a.id')
            ->join('cate c','c.id = b.cate_id')
            ->join('order d','d.pId = b.id','left')
            ->where('b.id', $pId)
            ->find();
	        $this->assign('pros', $detail);
	        // var_dump($detail);
	        $this->assign('exp0', $exp[0]['exp']);
	        $this->assign('exp1', $exp[1]['exp']);
	        $this->assign('time1', $time[0]['changeTime1']);
	        $this->assign('time2', $time[1]['changeTime2']);

        // var_dump($pId);	
        // var_dump($detail);		
  		return $this->fetch();
    }

    //更改页
    public function change($id,$pId)
    {
    	$all = Db::name('user')
            ->alias('a') 
            ->join('proinfo b','b.u_id = a.id')
            ->join('cate c','c.id = b.cate_id')
            ->join('order d','d.pId = b.id')
            ->where('d.pId', $pId)
            ->where('d.id', $id)
            ->find();
            
    	$request = $this->request;

    	if($request->isPost()){
            $post     = $request->post();
            $validate = validate('Order');
            $res      = $validate->check($post);
            $pId = $post['pId'];
            unset($post['pId']);

            if ($res !== true) {
                $this->error($validate->getError());
                
            }else{
            	//未处理工单更改
         		if($all['status'] == 1){
         			if($post['status'] == 1){
         				$this->error('请检查工单状态是否更改');
         			}elseif($post['status'] == 3)
         			{
         				$this->error('请不要跳过中间状态');
         			}else{
         				$db = Db::name('order')
	                    ->insert([
	                    	'resource'=>3,
	                    	'status'=>$post['status'],
	                    	'pId'=>$pId,
	                    	'exp'=>$post['exp'],
	                    	'changeTime1'=>date('Y-m-d H:i:s', time())
	                    ]); 
	                    $db2 = Db::name('order')
	                    ->where('pId',$pId)
	                    ->where('status',1)
	                    ->update(['exp'=>NULL]);
	                	$this->success('状态更改成功');
         			}
         		}
         		//处理中工单更改
         		elseif($all['status'] == 2) {
         			if($post['status'] == 1){
         				$this->error('工单状态有误！');
         			}elseif($post['status'] == 2){
         				$this->error('请检查工单状态是否更改');
         			}else{
         				$db = Db::name('order')
	                    ->insert([
	                    	'resource'=>3,
	                    	'status'=>$post['status'],
	                    	'pId'=>$pId,
	                    	'exp'=>$post['exp'],
	                    	'changeTime2'=>date('Y-m-d H:i:s', time())
	                    ]); 

	                	$this->success('状态更改成功');
         			}
         		}
            }
        }else{
            
	    $this->assign('pros', $all);

  		return $this->fetch();
        }

    	
    }
}